"""
预警编辑窗口
"""
from PySide6.QtCore import Slot, QSize, Qt, QModelIndex,QThread
from PySide6.QtGui import QAction, QKeySequence, QGuiApplication, QCloseEvent, QFont, QShortcut, QPalette
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QGridLayout, QPushButton, QWidget, QMessageBox,QDialog,QLabel, QComboBox, QTableView,QRadioButton,QPlainTextEdit,QTabWidget       ## , QStandardItemModel, QStandardItem, QHeaderView, QTableWidget, QTableWidgetItem, QAbstractItemView, QTableView, QTableWidgetItem, QTableWidgetSelectionRange, QTableWidgetItem, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidgetSelectionRange, QTableWidget,
from top.wardyan.core.action.baseaction import WMainWindow, WQdialog
from top.wardyan.core.vo.vos import *
from top.wardyan.core.widges.tables import WTableView
from top.wardyan.core.wdb.core.DBPool import POOL
from top.wardyan.core.wdb.core.db import *
from top.wardyan.core.widges.controlls import *
from top.wardyan.core.vo.const import *
from top.wardyan.core.sqlite.sqliteconfigs import readDicts, readDicts_reverse
import sys
from threading import Thread
import time

LBLWIDTH = 100
TXTWIDTH = 500
HEIGHT = 30

class WWarningEdit (WQdialog) :
    def __init__(self, warningID : int) -> None:
        ##global cn
        self.name = "warningedit"
        WQdialog.__init__(self, self.name)
        self.warningID = warningID
        self.table : WTableView = None
        ## 属性里的所有控件,用于遍历所有控件
        self.CONTROLS_PROPERTIES = []

        self.keywords = []
        # ks = query("SELECT top(1000) name FROM sys.objects AS o WHERE o.type IN ('U', 'P', 'V', 'TF', 'Fn')")
        # for k in ks :
        #     self.keywords.append(k["name"])

        self.initData()
        self.main()
        self.refreshTable()
        self.refreshControlls()

    def initData(self) :
        self.warning = find("select * from st_warning where sID = %s", (self.warningID,))
        if self.warning is None :
            QMessageBox.warning(self, "警告", "未找到预警项目", QMessageBox.StandardButton.Ok)
            self.close()
        title = self.warning["xName"]
        ## self.title = title
        self.setWindowTitle(title)
    def refreshControlls(self) :
        readonly = False
        if self.warning["sStatus"] > 2 :   
            readonly = True
        for w in self.CONTROLS_PROPERTIES :
            ## x : QWidget = w
            if hasattr(w, "setReadOnly") :
                if hasattr(w, "editable") :
                    if w.editable :
                        w.setReadOnly(readonly)
                    else :
                        w.setReadOnly(False)
                else :
                    w.setReadOnly(readonly)
            elif hasattr(w, "setDisabled") :
                ## readonly = not readonly     ## 这里需要反过来
                if hasattr(w, "editable") :
                    if w.editable :
                        w.setDisabled(readonly)
                    else :
                        w.setDisabled(False)
                else :
                    w.setDisabled(readonly)
            else :
                print("没有readonly属性", w)
        if readonly :
            ## self.btnSubmit.setDisabled(True)
            self.btnAudit.setText("编辑")
        else :
            ## self.btnSubmit.setDisabled(False)
            self.btnAudit.setText("审核")
        self.btnAudit.setDisabled(False)
        self.btnSubmit.setDisabled(True)

    def handle_double_click(self, index: QModelIndex) -> None:
        """
        处理单元格的双击事件。
        :param index: 被双击的单元格的模型索引。
        """
        row = index.row()
        column = index.column()
        data : Record = self.table.get_row_data(row)
        print(f"Double click detected at Row: {row}, Column: {column}", data)
        
    def on_tab_changed(self, index):
        """当标签页改变时，打印当前标签页的序号和标题"""
        current_widget = self.tabWidget.widget(index)
        title = self.tabWidget.tabText(index)
        print(f"当前标签页序号：{index}，标题：{title}")
    ## 公式编辑: 预警公式
    def init_warningsql_edit(self, wgt : QWidget) :
        """
        问题: 重复打开?
        """
        global LBLWIDTH
        global TXTWIDTH
        global HEIGHT
        lyt  = QVBoxLayout()
        wgt.setLayout(lyt)
        self.btnSaveWarningSQL : QPushButton = QPushButton("保存")
        self.btnSaveWarningSQL.setMinimumHeight(50)
        self.btnSaveWarningSQL.setFixedWidth(LBLWIDTH)
        self.btnSaveWarningSQL.setEnabled(False)
        self.btnSaveWarningSQL.clicked.connect(self.on_btnSaveWarningSQL_clicked)
        lyt.addWidget(self.btnSaveWarningSQL, alignment = Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignTop)
        ## TODO 如果单据是审核状态, 就不允许修改? 暂时不限制
        self.warningSQL : WCodeEdit = WCodeEdit(highlightkeywords=self.keywords)
        self.warningSQL.setPlainText(self.warning["xSQL"])
        font = QFont("新宋体", 12)  # 设置字体家族为Arial，字号为160
        self.warningSQL.setFont(font)
        lyt.addWidget(self.warningSQL)
        self.warningSQL.textChanged.connect(self.handle_warningsql_TextChanged)
        self.warningSQL.saveShortcut = QShortcut(QKeySequence.StandardKey.Save, self.warningSQL)
        self.warningSQL.saveShortcut.activated.connect(self.handle_warningsql_saveShortcut)
        
    def on_btnSaveWarningSQL_clicked(self) :
        sql = "update st_warning set xSQL = %s, xFormula = %s where sID = %d"
        exec(sql, (self.warningSQL.toPlainText(), self.warningSQL.toPlainText(), self.warningID))
        sql = "update st_warning set xLen = len(xSQL) where sID = %s"
        exec(sql, (self.warningID))        
        self.btnSaveWarningSQL.setEnabled(False)

    @Slot()
    def handle_warningsql_TextChanged(self):
        # 在这里放置当文本变化时需要执行的代码
        self.btnSaveWarningSQL.setEnabled(True)

    def handle_warningsql_saveShortcut(self) :
        self.on_btnSaveWarningSQL_clicked()

    ## 公式编辑: 卡片详情
    def init_cardinfosql_edit(self, wgt : QWidget) :
        """
        问题: 重复打开?
        """
        global LBLWIDTH
        global TXTWIDTH
        global HEIGHT

        lyt  = QVBoxLayout()
        wgt.setLayout(lyt)
        self.btnSaveCardInfoSQL : QPushButton = QPushButton("保存")
        self.btnSaveCardInfoSQL.setMinimumHeight(50)
        self.btnSaveCardInfoSQL.setFixedWidth(LBLWIDTH)
        self.btnSaveCardInfoSQL.setEnabled(False)
        self.btnSaveCardInfoSQL.clicked.connect(self.on_btnSaveCardInfoSQL_clicked)
        lyt.addWidget(self.btnSaveCardInfoSQL, alignment = Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignTop)
        ## TODO 如果单据是审核状态, 就不允许修改? 暂时不限制
        self.cardinfoSQL : WCodeEdit = WCodeEdit(highlightkeywords=self.keywords)
        self.cardinfoSQL.setPlainText(self.warning["xAlarmTypeSQL"])
        font = QFont("新宋体", 12)  # 设置字体家族为Arial，字号为160
        self.cardinfoSQL.setFont(font)
        lyt.addWidget(self.cardinfoSQL)
        self.cardinfoSQL.textChanged.connect(self.handle_cardinfo_TextChanged)
        self.cardinfoSQL.saveShortcut = QShortcut(QKeySequence.StandardKey.Save, self.cardinfoSQL)
        self.cardinfoSQL.saveShortcut.activated.connect(self.handle_cardinfo_saveShortcut)
        ## self.cardinfoSQL.activeIt()
        ## self.ActiveWCoderEdit(self, self.cardinfoSQL).start()
    
    def on_btnSaveCardInfoSQL_clicked(self) :
        sql = "update st_warning set xAlarmTypeSQL = %s, xAlarmTypeSQLFormula = %s where sID = %d"
        exec(sql, (self.cardinfoSQL.toPlainText(), self.warningSQL.toPlainText(), self.warningID))
        sql = "update st_warning set xAlarmTypeLen = len(xAlarmTypeSQL) where sID = %s"
        exec(sql, (self.warningID))        
        self.btnSaveCardInfoSQL.setEnabled(False)

    @Slot()
    def handle_cardinfo_TextChanged(self):
        # 在这里放置当文本变化时需要执行的代码
        self.btnSaveCardInfoSQL.setEnabled(True)

    def handle_cardinfo_saveShortcut(self) :
        self.on_btnSaveCardInfoSQL_clicked()

    ## 公式编辑: 接收人
    def init_targettype_edit(self, wgt : QWidget) :
        """
        问题: 重复打开?
        """
        global LBLWIDTH
        global TXTWIDTH
        global HEIGHT

        lyt  = QVBoxLayout()
        wgt.setLayout(lyt)
        self.btnSaveTargetTypeSQL : QPushButton = QPushButton("保存")
        self.btnSaveTargetTypeSQL.setMinimumHeight(50)
        self.btnSaveTargetTypeSQL.setFixedWidth(LBLWIDTH)
        self.btnSaveTargetTypeSQL.setEnabled(False)
        self.btnSaveTargetTypeSQL.clicked.connect(self.on_btnSaveTargetTypeSQL_clicked)
        lyt.addWidget(self.btnSaveTargetTypeSQL, alignment = Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignTop)
        ## TODO 如果单据是审核状态, 就不允许修改? 暂时不限制
        self.targettypeSQL : WCodeEdit = WCodeEdit(highlightkeywords=self.keywords)
        self.targettypeSQL.setPlainText(self.warning["xTargetTypeSQL"])
        font = QFont("新宋体", 12)  # 设置字体家族为Arial，字号为160
        self.targettypeSQL.setFont(font)
        lyt.addWidget(self.targettypeSQL)
        self.targettypeSQL.textChanged.connect(self.handle_targettype_TextChanged)
        self.targettypeSQL.saveShortcut = QShortcut(QKeySequence.StandardKey.Save, self.targettypeSQL)
        self.targettypeSQL.saveShortcut.activated.connect(self.handle_targettype_saveShortcut)
        ## self.targettypeSQL.activeIt()
        ## self.ActiveWCoderEdit(self, self.targettypeSQL).start()
    
    def on_btnSaveTargetTypeSQL_clicked(self) :
        sql = "update st_warning set xTargetTypeSQL = %s, xTargetTypeSQLFormula = %s where sID = %d"
        exec(sql, (self.targettypeSQL.toPlainText(), self.warningSQL.toPlainText(), self.warningID))
        sql = "update st_warning set xTargetTypeLen = len(xTargetTypeSQL) where sID = %s"
        exec(sql, (self.warningID))        
        self.btnSaveTargetTypeSQL.setEnabled(False)

    @Slot()
    def handle_targettype_TextChanged(self):
        # 在这里放置当文本变化时需要执行的代码
        self.btnSaveTargetTypeSQL.setEnabled(True)
    def handle_targettype_saveShortcut(self) :
        self.on_btnSaveTargetTypeSQL_clicked()


    def init_properties_edit(self, wgt : QWidget) :
        """
        问题: 重复打开?
        """
        global LBLWIDTH
        global TXTWIDTH
        global HEIGHT

        lyt  = QHBoxLayout()
        wgt.setLayout(lyt)
        
        editWidget : QWidget = QWidget()
        ##editWidget.setFixedWidth(800)
        editWidget.setFixedSize(1024, 480)
        lyt.addWidget(editWidget)

        lytGrid : QGridLayout = QGridLayout()
        ## lytGrid.setcolumn
        lytGrid.setColumnStretch(0, 1)
        lytGrid.setColumnStretch(1, 5)
        lytGrid.setColumnStretch(2, 1)
        lytGrid.setColumnStretch(3, 5)
        editWidget.setLayout(lytGrid)

        ## TODO 如果单据是审核状态, 就不允许修改

        
        lytButtons : QHBoxLayout = QHBoxLayout()
        wgtButtons : QWidget = QWidget()
        wgtButtons.setLayout(lytButtons)


        ## TODO 如果有改动了 又没有保存, 则关闭时要提醒
        self.btnClose : QPushButton = QPushButton("关闭")
        self.btnClose.setMinimumHeight(50)
        self.btnClose.setFixedWidth(LBLWIDTH)
        self.btnClose.setDefault(True)
        self.btnClose.clicked.connect(self.on_btnClose_clicked)
        ##lytGrid.addWidget(self.btnClose,8, 2, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        lytButtons.addWidget(self.btnClose)
        
        btntxt = "审核"
        if self.warning["sStatus"] >= 5 :
            btntxt = "编辑"
        self.btnAudit : QPushButton = QPushButton(btntxt)
        self.btnAudit.setMinimumHeight(50)
        self.btnAudit.setFixedWidth(LBLWIDTH)
        ## self.btnSubmit.setDefault(True)
        self.btnAudit.clicked.connect(self.on_btnAudit_clicked)
        lytButtons.addWidget(self.btnAudit)
            
        self.btnSubmit : QPushButton = QPushButton("保存")
        if self.warning["sStatus"] > 2 :
            self.btnSubmit.setEnabled(False)
        self.btnSubmit.setMinimumHeight(50)
        self.btnSubmit.setFixedWidth(LBLWIDTH)
        ## self.btnSubmit.setDefault(True)
        self.btnSubmit.clicked.connect(self.on_btnSubmit_clicked)
        lytButtons.addWidget(self.btnSubmit)
        lytGrid.addWidget(wgtButtons,8,3, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        self.btnClose.setFocus()
        self.btnSubmit.setEnabled(False)
        ## 启用 保存 删除 审核




        ## sID应该是不允许编辑的
        ## 新增时才允许编辑? 不, 新增时自动生成
        lblIdx, self.txtIdx = add_line_edit_integer("ID", str(self.warning["sID"]))
        self.txtIdx.setReadOnly(True)
        self.txtIdx.editable=False
        # 设置不可编辑状态下的背景颜色
        palette = self.txtIdx.palette()
        palette.setColor(QPalette.ColorRole.Base, QColor(240, 240, 240))
        self.txtIdx.setPalette(palette)
        lytGrid.addWidget(lblIdx,0,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtIdx,0,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        ## self.txtIdx.editable = False
        ## 不参与控件的变换状态
        ## self.CONTROLS_PROPERTIES.append(self.txtIdx)

        lblCode, self.txtCode = add_line_edit("代码", self.warning["sCode"])
        lytGrid.addWidget(lblCode,0,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtCode,0,3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.txtCode.textChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.txtCode)
        
        
        lblKind, self.txtKind = add_line_edit("类型", self.warning["xKind"])
        lytGrid.addWidget(lblKind,1,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtKind,1,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.txtKind.textChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.txtKind)
        

        lblName, self.txtName = add_line_edit("名称", self.warning["xName"])
        lytGrid.addWidget(lblName,1,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtName,1,3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.txtName.textChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.txtName)

        dict_level = readDicts_reverse(1001)
        lblLevel, self.cmbLevel = add_commbobox("级别", self.warning["xLevel"], dict_level)
        lytGrid.addWidget(lblLevel,2,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbLevel,2,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbLevel.editTextChanged.connect(self.handle_properties_change)
        ## self.cmbLevel.setDisabled()
        self.CONTROLS_PROPERTIES.append(self.cmbLevel)

        dict_datatype = readDicts_reverse(1002)
        ## lblDataType, self.txtDataType = add_line_edit("检查周期", "")
        lblDataType, self.cmbDatatype = add_commbobox("检查周期", self.warning["xDataType"], dict_datatype)
        lytGrid.addWidget(lblDataType,2,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbDatatype,2, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbDatatype.editTextChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.cmbDatatype)
        
        dict_alarmtype = readDicts_reverse(1003)
        ## lblAlarmType, self.txtAlarmType = add_line_edit("预警方式", "")
        lblAlarmType, self.cmbAlarmtype = add_commbobox("预警方式", self.warning["xAlarmType"], dict_alarmtype)
        lytGrid.addWidget(lblAlarmType,3,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbAlarmtype,3,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbAlarmtype.editTextChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.cmbAlarmtype)
        
        dict_sendtype = readDicts(1007)
        lblSendType, self.bitSendType = add_bitcheckgroup("发送类型", self.warning["xSendType"], dict_sendtype, onchange=self.handle_properties_change)
        ## self.bitSendType.setValue(self.warning["xSendType"])
        lytGrid.addWidget(lblSendType,3,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.bitSendType,3, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        ## self.bitSendType.onchange = self.handle_properties_change
        self.CONTROLS_PROPERTIES.append(self.bitSendType)

        # lblAlarmType, self.txtAlarmType = add_line_edit("最少触发次数", "")
        # lytGrid.addWidget(lblAlarmType,3,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        # lytGrid.addWidget(self.txtAlarmType,3,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        # lblSendType, self.txtSendType = add_line_edit("预警最少间隔秒", "")
        # lytGrid.addWidget(lblSendType,3,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        # lytGrid.addWidget(self.txtSendType,3, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        dict_targettype = readDicts_reverse(1004)
        ## lblTargetType, self.txtTargetType = add_line_edit("接收目标", "")
        lblTargetType, self.cmbTargetType = add_commbobox("接收目标", self.warning["xTargetType"], dict_targettype)
        lytGrid.addWidget(lblTargetType,5,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbTargetType,5,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbTargetType.editTextChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.cmbTargetType)

        dict_icon = readDicts_reverse(1005)
        ## lblIcon, self.txtIcon = add_line_edit("图标", "")
        lblIcon, self.cmbIcon = add_commbobox("图标", self.warning["xIcon"], dict_icon)
        lytGrid.addWidget(lblIcon,5,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbIcon,5, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbIcon.editTextChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.cmbIcon)
        
        dict_msgtype = readDicts_reverse(1006)
        ## lblMsgType, self.txtMsgType = add_line_edit("消息类型", "")
        lblMsgType, self.cmbMsgType = add_commbobox("消息类型", self.warning["xMsgType"], dict_msgtype)
        lytGrid.addWidget(lblMsgType,6,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.cmbMsgType,6,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.cmbMsgType.editTextChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.cmbMsgType)

        lblDate, self.txtDate = add_line_edit("创建日期", "")
        self.txtDate.setReadOnly(True)
        lytGrid.addWidget(lblDate,6,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtDate,6, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.txtDate.textChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.txtDate)

        lblTableName, self.txtTableName = add_line_edit("单据主表", "")
        lytGrid.addWidget(lblTableName,7,0, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        lytGrid.addWidget(self.txtTableName,7,1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.txtTableName.textChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.txtTableName)

        # lblIsDedicate, self.txtIsDedicate = add_line_edit("专用", "")
        # lytGrid.addWidget(lblIsDedicate,7,2, Qt.AlignmentFlag.AlignRight | Qt.AlignmentFlag.AlignVCenter)
        # lytGrid.addWidget(self.txtIsDedicate,7, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.chkIsDedicate : WCheckBox = WCheckBox()
        self.chkIsDedicate.setText("专用")
        self.chkIsDedicate.setChecked(self.warning["xIsDedicate"])
        lytGrid.addWidget(self.chkIsDedicate,7, 3, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.chkIsDedicate.stateChanged.connect(self.handle_properties_change)
        self.CONTROLS_PROPERTIES.append(self.chkIsDedicate)
        
        
        ## 保存 关闭 删除
        self.btnDelete : QPushButton = QPushButton("删除")
        self.btnDelete.setMinimumHeight(50)
        self.btnDelete.setFixedWidth(LBLWIDTH)
        self.btnDelete.clicked.connect(self.on_btnDelete_clicked)
        lytGrid.addWidget(self.btnDelete,8, 1, Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)

        self.refreshControlls()

        lytGrid.activate()



    ## @Slot()
    def handle_properties_change(self) :
        self.btnSubmit.setEnabled(True)
        self.btnAudit.setEnabled(False)

    def on_btnDelete_clicked(self) :
        if self.warning["sStatus"] > 2 :
            ## 审核
            QMessageBox.warning(self, "警告", "非编辑状态下不允许删除", QMessageBox.StandardButton.Ok)
        else : 
            ## 删除
            exec("delete from dbo.st_warning where sID = %s", (self.warningID,))
            QMessageBox.warning(self, "提示", "删除成功", QMessageBox.StandardButton.Ok)
            self.close()
    def on_btnClose_clicked(self) :
        self.close()

    def on_btnAudit_clicked(self) :
        action = self.warning["sStatus"]
        if action > 2 :
            ## 取消审核
            exec("update dbo.st_warning set sStatus = 2 where sID = %s", (self.warningID,))
        else : 
            ## 审核
            exec("update dbo.st_warning set sStatus = 5 where sID = %s", (self.warningID,))
        ## QMessageBox.warning(self, "提示", "保存成功", QMessageBox.StandardButton.Ok)
        self.initData()
        self.refreshControlls()

    def on_btnSubmit_clicked(self) :
        if self.warning["sStatus"] > 2 :
            ## 审核
            QMessageBox.warning(self, "警告", "非编辑状态下不允许编辑", QMessageBox.StandardButton.Ok)
        else : 
            ## 保存
            sCode = self.txtCode.text()
            xDataType = self.cmbDatatype.getSelectedValue()
            xKind = self.txtKind.text()
            xName = self.txtName.text()
            xLevel = self.cmbLevel.getSelectedValue()
            xAlarmType = self.cmbAlarmtype.getSelectedValue()
            xTargetType = self.cmbTargetType.getSelectedValue()
            xMsgType = self.cmbMsgType.getSelectedValue()
            xIcon = self.cmbIcon.getSelectedValue()
            xSendType = self.bitSendType.getValue()
            xIsDedicate = self.chkIsDedicate.isChecked()
            sql = "update dbo.st_warning set sCode = %s, xDataType = %s, xKind = %s, xName = %s, xLevel = %s, xAlarmType = %s, xTargetType = %s, xMsgType = %s, xIcon = %s, xSendType = %s, xIsDedicate = %s, sLastUpdate = getdate() where sID = %s"
            exec(sql, (sCode, xDataType, xKind, xName, xLevel, xAlarmType, xTargetType, xMsgType, xIcon, xSendType, xIsDedicate, self.warningID))
            QMessageBox.warning(self, "提示", "保存成功", QMessageBox.StandardButton.Ok)
            self.btnSubmit.setDisabled(True)
            self.btnAudit.setDisabled(False)
            self.initData()

    def main(self) :
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)
        ##
        self.tabWidget : QTabWidget = QTabWidget()
        layout.addWidget(self.tabWidget)

        self.wgtPropertiesEdit : QWidget = QWidget()
        self.tabWidget.addTab(self.wgtPropertiesEdit, "属性编辑")
        self.init_properties_edit(self.wgtPropertiesEdit)
        
        self.wgtWarningEdit : QWidget = QWidget()
        self.tabWidget.addTab(self.wgtWarningEdit, "预警公式编辑")
        self.init_warningsql_edit(self.wgtWarningEdit)
        self.wgtShowEdit : QWidget = QWidget()
        self.tabWidget.addTab(self.wgtShowEdit, "显示公式编辑")
        self.init_cardinfosql_edit(self.wgtShowEdit)
        ##
        self.wgtReceiverEdit : QWidget = QWidget()
        self.tabWidget.addTab(self.wgtReceiverEdit, "接收人公式编辑")
        self.init_targettype_edit(self.wgtReceiverEdit)

        self.tabWidget.currentChanged.connect(self.on_tab_changed)

    def refreshTable(self) :
        pass
        
        
